package com.example.mylibrary.utils

import android.content.Context
import android.content.Context.TELEPHONY_SERVICE
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import android.text.TextUtils
import java.io.UnsupportedEncodingException
import java.util.*


object EquipmentUtil {
    /**
     * 获取当前手机系统语言。
     *
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    fun getSystemLanguage(): String? {
        return Locale.getDefault().getLanguage()
    }

    /**
     * 获取当前系统上的语言列表(Locale列表)
     *
     * @return  语言列表
     */
    fun getSystemLanguageList(): Array<Locale?>? {
        return Locale.getAvailableLocales()
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    fun getSystemVersion(): String? {
        return Build.VERSION.RELEASE
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    fun getSystemModel(): String? {
        return Build.MODEL
    }

    /**
     * 获取手机设备名
     *
     * @return  手机设备名
     */
    fun getSystemDevice(): String? {
        return Build.DEVICE
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    fun getDeviceBrand(): String? {
        return Build.BRAND
    }

    /**
     * 获取手机主板名
     *
     * @return  主板名
     */
    fun getDeviceBoand(): String? {
        return Build.BOARD
    }


    /**
     * 获取手机厂商名
     *
     * @return  手机厂商名
     */
    fun getDeviceManufacturer(): String? {
        return Build.MANUFACTURER
    }


    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return  手机IMEI
     */
    fun getIMEI(ctx: Context?): String? {

        val tm = (ctx?.getSystemService(TELEPHONY_SERVICE)) as TelephonyManager
        val deviceId = tm.getDeviceId()
        if (deviceId.isNullOrEmpty()) {
            return Settings.System.getString(ctx.contentResolver, Settings.Secure.ANDROID_ID)
        }
        return deviceId
    }

    fun getDeviceId(context: Context): String {
        val targetSdkVersion = context.applicationInfo.targetSdkVersion
        return if (targetSdkVersion > Build.VERSION_CODES.P && Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
            Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        } else {
            getTelId(context)
        }
    }

    private fun getTelId(context: Context): String {
        val manager = context.getSystemService(TELEPHONY_SERVICE) as TelephonyManager
        return manager.deviceId
    }

    private fun getUniqueID(context: Context): String? {
        var id: String? = null
        val androidId =
            Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        if (!TextUtils.isEmpty(androidId) && "9774d56d682e549c" != androidId) {
            try {
                val uuid = UUID.nameUUIDFromBytes(androidId.toByteArray(charset("utf8")))
                id = uuid.toString()
            } catch (e: UnsupportedEncodingException) {
                e.printStackTrace()
            }
        }
        if (TextUtils.isEmpty(id)) {
            id = getUUID()
        }
        return if (TextUtils.isEmpty(id)) UUID.randomUUID().toString() else id
    }

    private fun getUUID(): String? {
        var serial: String? = null
        val m_szDevIDShort =
            "35" + Build.BOARD.length % 10 + Build.BRAND.length % 10 + (if (null != Build.CPU_ABI) Build.CPU_ABI.length else 0) % 10 + Build.DEVICE.length % 10 + Build.DISPLAY.length % 10 + Build.HOST.length % 10 + Build.ID.length % 10 + Build.MANUFACTURER.length % 10 + Build.MODEL.length % 10 + Build.PRODUCT.length % 10 + Build.TAGS.length % 10 + Build.TYPE.length % 10 + Build.USER.length % 10 //13 位
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            try {
                serial = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    Build.getSerial()
                } else {
                    Build.SERIAL
                }
                //API>=9 使用 serial 号
                return UUID(
                    m_szDevIDShort.hashCode().toLong(),
                    serial.hashCode().toLong()
                ).toString()
            } catch (exception: Exception) {
                serial = "serial" // 随便一个初始化
            }
        } else {
            serial = Build.UNKNOWN // 随便一个初始化
        }

        // 使用硬件信息拼凑出来的 15 位号码
        return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
    }
}